/*    */ package com.chw.admin.login;
/*    */ 
/*    */ import java.awt.Graphics;
/*    */ import java.awt.image.BufferedImage;
/*    */ import java.io.IOException;
/*    */ import javax.imageio.ImageIO;
/*    */ import javax.servlet.ServletContext;
/*    */ import javax.servlet.ServletException;
/*    */ import javax.servlet.ServletOutputStream;
/*    */ import javax.servlet.http.HttpServlet;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import javax.servlet.http.HttpServletResponse;
/*    */ import javax.servlet.http.HttpSession;
/*    */ import javax.servlet.http.HttpSessionContext;
/*    */ import org.apache.log4j.Logger;
/*    */ 
/*    */ public class CreateImage extends HttpServlet
/*    */ {
/* 22 */   private static Logger log = Logger.getLogger(CreateImage.class);
/*    */   private static final String CONTENT_TYPE = "text/html; charset=utf-8";
/* 24 */   final String input_back_color_error = "input rgb backcolor is error";
/*    */ 
/* 26 */   final String input_fore_color_error = "input rgb forecolor is error";
/*    */ 
/* 28 */   private Picture pic = new Picture();
/* 29 */   private String realpath = "";
/*    */ 
/*    */   public void init()
/*    */     throws ServletException
/*    */   {
/* 34 */     this.realpath = getServletContext().getRealPath("/images/user/codebakgroup.jpg");
/*    */   }
/*    */ 
/*    */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*    */     throws ServletException, IOException
/*    */   {
/* 40 */     doGet(request, response);
/*    */   }
/*    */ 
/*    */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*    */     throws ServletException, IOException
/*    */   {
/* 47 */     Picture pic = new Picture();
/*    */ 
/* 49 */     String SessionId = request.getParameter("sessionId");
/* 50 */     BufferedImage image = null;
/* 51 */     ServletOutputStream out = null;
/* 52 */     HttpSession Sess = null;
/* 53 */     StringBuffer keyBuf = null;
/* 54 */     if (SessionId == null) {
/* 55 */       HttpSessionContext SessCon = request.getSession(true).getSessionContext();
/* 56 */       Sess = SessCon.getSession(SessionId);
/*    */ 
/* 58 */       if (Sess == null)
/*    */       {
/* 61 */         Sess = request.getSession(true);
/*    */       }
/*    */     }
/*    */     try
/*    */     {
/* 66 */       image = pic.getBufferedImage();
/* 67 */       Graphics g = image.getGraphics();
/* 68 */       pic.getbackground(g, 0, 0);
/* 69 */       keyBuf = new StringBuffer();
/* 70 */       int offset = 0;
/* 71 */       int num = pic.getcharacterNumber();
/*    */ 
/* 73 */       for (int i = 0; i < num; i++) {
/* 74 */         int ri = (int)(Math.random() * 10.0D);
/* 75 */         keyBuf.append(ri);
/* 76 */         pic.getcharacter(g, offset, ri);
/* 77 */         offset += 60 / num;
/*    */       }
/* 79 */       pic.getInterrupt(g);
/* 80 */       response.setHeader("Cache-Control", "no-store");
/* 81 */       response.setDateHeader("Expires", 0L);
/* 82 */       response.setContentType("image/jpeg");
/* 83 */       out = response.getOutputStream();
/* 84 */       ImageIO.write(image, "jpg", out);
/*    */ 
/* 87 */       log.info("创建验证码成功!");
/*    */ 
/* 91 */       if (null != image) {
/* 92 */         image.flush();
/*    */       }
/* 94 */       if (null != out) {
/* 95 */         out.flush();
/* 96 */         out.close();
/*    */       }
/*    */     }
/*    */     catch (Exception e)
/*    */     {
/* 89 */       e.printStackTrace();
/*    */ 
/* 91 */       if (null != image) {
/* 92 */         image.flush();
/*    */       }
/* 94 */       if (null != out) {
/* 95 */         out.flush();
/* 96 */         out.close();
/*    */       }
/*    */     }
/*    */     finally
/*    */     {
/* 91 */       if (null != image) {
/* 92 */         image.flush();
/*    */       }
/* 94 */       if (null != out) {
/* 95 */         out.flush();
/* 96 */         out.close();
/*    */       }
/*    */     }
/* 99 */     Sess.setAttribute("randomstr", keyBuf.toString());
/*    */   }
/*    */ }

/* Location:           E:\chenya\2017 4-21\com.chw\admin\login\
 * Qualified Name:     com.chw.admin.login.CreateImage
 * JD-Core Version:    0.6.0
 */